#include <iostream>
#include <memory>
#include "TcpServer.hpp"
#include "Usage.hpp"
#include "Comm.hpp"

void Unknown(int sockfd, InetAddr &addr)
{
    std::string message = "sorry, we don't have this service, choose another service please!";
    ssize_t n = write(sockfd, &message, message.size());
    if (n < 0) // 发送错误
    {
        lg.LogMessage(Error, "write failed, errno %d is %s", errno, strerror(errno));
        exit(WRITE_ERROR);
    }
    else if (n == 0)
    {
        lg.LogMessage(Info, "client quit..\n");
        exit(WRITE_ERROR);
    }
}

void Translate(int sockfd, InetAddr &addr)
{
    
}

void Ping(int sockfd, InetAddr &addr)
{
}

void Transform(int sockfd, InetAddr &addr)
{
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return USAGE_ERROR;
    }
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(port);
    tsvr->RegistrationService("unknown", Unknown);
    tsvr->RegistrationService("translate", Translate);
    tsvr->RegistrationService("ping", Ping);
    tsvr->RegistrationService("transform", Transform);
    tsvr->Init();
    tsvr->Start();
    return 0;
}